package service

import (
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/errcode"
)

func (svc *Service) GetUserList(pager *app.Pager) (int, []*model.UserRes, *errcode.Error) {
	totalRows, err := svc.dao.CountUser()
	if err != nil {
		global.Logger.Errorf("svc.CountUser err: %v", err)
		return 0, nil, errcode.ErrorCountUserFail.WithDetails(err.Error())
	}
	users, err := svc.dao.GetUserList(pager.Page, pager.PageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetUserList err: %v", err)
		return 0, nil, errcode.ErrorGetUserListFail.WithDetails(err.Error())
	}
	return int(totalRows), users, nil
}

func (svc *Service) FindUserById(param *model.FindUserByIdRequest) (*model.UserRes, error) {
	return svc.dao.FindUserById(param.ID)
}

func (svc *Service) CreateUser(param *model.CreateUserRequest) error {
	return svc.dao.CreateUser(param.Name, param.Email, param.Password)
}

func (svc *Service) UpdateUser(param *model.UpdateUserRequest) error {
	return svc.dao.UpdateUser(param)
}

func (svc *Service) DeleteUser(param *model.DeleteUserRequest) error {
	return svc.dao.DeleteUser(param.ID)
}
